package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import com.zretc.device.dao.DeviceDao;
import com.zretc.device.entity.Device;

import javax.swing.DefaultComboBoxModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPopupMenu;
import java.awt.Component;
import javax.swing.JMenuItem;

public class DeviceFindDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtInput;
	private JTable table;
	private JTextField txtdevice_code;
	private JTextField txtdevice_name;
	private JTextField txtnow_num;
	private JTextField txthigh_num;
	private JTextField txtlow_num;
	private JTextField txttotal_num;
	private JScrollPane scrollPane;
	private String devCode;
	private JPopupMenu popupMenu;
	private JMenuItem mntmNewMenuItem;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DeviceFindDialog dialog = new DeviceFindDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public DeviceFindDialog() {
		setBounds(100, 100, 792, 577);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel label = new JLabel("请选择查询方式");
		label.setFont(new Font("宋体", Font.PLAIN, 25));
		label.setBounds(24, 37, 181, 38);
		contentPanel.add(label);

		JLabel label_1 = new JLabel("请输入查询信息");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(24, 88, 181, 38);
		contentPanel.add(label_1);

		JComboBox txtCB = new JComboBox();
		txtCB.setModel(new DefaultComboBoxModel(new String[] {"设备编号", "设备名称"}));
		txtCB.setFont(new Font("宋体", Font.PLAIN, 20));
		txtCB.setBounds(218, 43, 202, 30);
		contentPanel.add(txtCB);

		txtInput = new JTextField();
		txtInput.setText("  ");
		txtInput.setFont(new Font("宋体", Font.PLAIN, 25));
		txtInput.setColumns(10);
		txtInput.setBounds(219, 91, 211, 31);
		contentPanel.add(txtInput);

		JButton button = new JButton("查询");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String opString = txtCB.getSelectedItem() + "";
				// 取值
				String code = txtInput.getText();
				// 对复选框的内容进行判断
				if (opString.equals("设备编号")) {
					if (code.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "查询内容不能为空");
					} else {
						// 调用方法
						Device dev = fillTableByCo(scrollPane, code);
						if (dev == null) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该编号不存在，请重新输入");
						}
					}
				} else if (opString.equals("设备名称")) {
					if (code.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "查询内容不能为空");
					} else {
						// 调用方法
						List<Device> list=fillTableByName(scrollPane, code);
						if (list.isEmpty()) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该名称不存在，请重新输入");
						}
					}
				}
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 20));
		button.setBounds(463, 94, 113, 31);
		contentPanel.add(button);

		JButton button_1 = new JButton("取消");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtInput.setText("");
				fillTable();
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 20));
		button_1.setBounds(580, 95, 113, 31);
		contentPanel.add(button_1);

		scrollPane = new JScrollPane();
		scrollPane.setBounds(14, 210, 679, 263);
		contentPanel.add(scrollPane);

		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				//取值
				devCode = table.getValueAt(table.rowAtPoint(e.getPoint()), 0) + "";
				String devName = table.getValueAt(table.rowAtPoint(e.getPoint()), 1) + "";
				String nowNumString = table.getValueAt(table.rowAtPoint(e.getPoint()), 2) + "";
				String highNumString = table.getValueAt(table.rowAtPoint(e.getPoint()), 3) + "";
				String lowNumString = table.getValueAt(table.rowAtPoint(e.getPoint()), 4) + "";
				String totalNumString = table.getValueAt(table.rowAtPoint(e.getPoint()), 5) + "";
			
				//赋值
				txtdevice_code.setText(devCode);
				txtdevice_name.setText(devName);
				txtnow_num.setText(nowNumString);
				txthigh_num.setText(highNumString);
				txtlow_num.setText(lowNumString);
				txttotal_num.setText(totalNumString);
			}
		});
		table.setRowHeight(25);
		
		popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);
		
		mntmNewMenuItem = new JMenuItem("删除");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int n = JOptionPane.showConfirmDialog(null, "请问是否继续删除？删除后将无法恢复！", "删除设备编号为: " + devCode + " 的所有信息",
						JOptionPane.YES_NO_OPTION);
				if (n == 0) {
					// 创建对象
					DeviceDao dd = new DeviceDao();
					boolean flag = dd.delDev(devCode);
					if (flag) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "删除成功");
					} else {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "删除失败", "消息", JOptionPane.ERROR_MESSAGE);
					}
					// 更新
					fillTable();
				}
			}
		});
		popupMenu.add(mntmNewMenuItem);
		fillTable();

		txtdevice_code = new JTextField();
		txtdevice_code.setEditable(false);
		txtdevice_code.setColumns(10);
		txtdevice_code.setBounds(34, 150, 86, 30);
		contentPanel.add(txtdevice_code);

		txtdevice_name = new JTextField();
		txtdevice_name.setColumns(10);
		txtdevice_name.setBounds(117, 150, 86, 30);
		contentPanel.add(txtdevice_name);

		txtnow_num = new JTextField();
		txtnow_num.setColumns(10);
		txtnow_num.setBounds(202, 150, 86, 30);
		contentPanel.add(txtnow_num);

		txthigh_num = new JTextField();
		txthigh_num.setColumns(10);
		txthigh_num.setBounds(285, 150, 86, 30);
		contentPanel.add(txthigh_num);

		txtlow_num = new JTextField();
		txtlow_num.setColumns(10);
		txtlow_num.setBounds(370, 150, 86, 30);
		contentPanel.add(txtlow_num);

		txttotal_num = new JTextField();
		txttotal_num.setColumns(10);
		txttotal_num.setBounds(455, 150, 86, 30);
		contentPanel.add(txttotal_num);

		JButton button_2 = new JButton("确认修改");
		button_2.setFont(new Font("宋体", Font.PLAIN, 20));
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				// 获取文本框里的值
				// 设备id
				String device_code = txtdevice_code.getText();
				// 设备名称
				String device_name = txtdevice_name.getText();
				// 现有库存,转型
				String tnow_num = txtnow_num.getText();
				// 最大库存
				String thigh_num = txthigh_num.getText();
				// 最小库存
				String tlow_num = txtlow_num.getText();
				// 总数
				String ttotal_num = txttotal_num.getText();
				// 现有库存,转型
				int now_num = Integer.parseInt(tnow_num);
				// 最大库存
				int high_num = Integer.parseInt(thigh_num);
				// 最小库存
				int low_num = Integer.parseInt(tlow_num);
				// 总数
				int total_num = Integer.parseInt(ttotal_num);
				// 判断输入的值
				if (device_code.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "编号不能为空");
				} else if (device_name.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "名称不能为空");
				} else if (tnow_num.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "现有库存不能为空");
				} else if (thigh_num.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "最大库存不能为空");
				} else if (tlow_num.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "最低库存不能为空");
				} else if (ttotal_num.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "库存总数不能为空");
				} else {
					// 创建对象
					Device device = new Device(device_code, device_name, now_num, high_num, low_num, total_num);
					DeviceDao dd = new DeviceDao();
					boolean flag = dd.updateDev(device);
					if (flag) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "修改成功");
						fillTable();
					} else {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "修改失败");
					}
				}
			}
		});
		button_2.setBounds(572, 150, 121, 27);
		contentPanel.add(button_2);
		// 居中
		setLocationRelativeTo(null);
	}
	/**
	 * 查询所有
	 */
	private void fillTable() {
		// 创建对象
		DeviceDao dd = new DeviceDao();
		// 调用查找所有的方法
		List<Device> list = dd.findDeviceAll();
		// 获取list长度
		int row = list.size();
		// 创建二维数组
		Object[][] obj = new Object[row][6];
		// 迭代遍历
		for (int i = 0; i < obj.length; i++) {
			// 取值
			Device device = list.get(i);
			// 赋值
			obj[i][0] = device.getDevice_code();
			obj[i][1] = device.getDevice_name();
			obj[i][2] = device.getNow_num();
			obj[i][3] = device.getHigh_num();
			obj[i][4] = device.getLow_num();
			obj[i][5] = device.getTotal_num();
		}
		table.setModel(new DefaultTableModel(obj, new String[] { "\u8BBE\u5907\u7F16\u53F7", "\u8BBE\u5907\u540D\u79F0",
				"\u73B0\u6709\u5E93\u5B58", "\u6700\u5927\u5E93\u5B58", "\u6700\u5C0F\u5E93\u5B58", "\u603B\u6570" }));
		scrollPane.setViewportView(table);
	}

	/**
	 * 通过编号查询，并输出在表格里
	 * 
	 * @param scrollPane
	 */
	private Device fillTableByCo(JScrollPane scrollPane, String code) {
		// 创建对象
		DeviceDao dd = new DeviceDao();
		// 调用查找de方法
		Device device = dd.findDevByCo(code);
		//
		if (device != null) {
			// 创建二维数组
			Object[][] obj = new Object[1][6];
			// 赋值
			obj[0][0] = device.getDevice_code();
			obj[0][1] = device.getDevice_name();
			obj[0][2] = device.getNow_num();
			obj[0][3] = device.getHigh_num();
			obj[0][4] = device.getLow_num();
			obj[0][5] = device.getTotal_num();

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u8BBE\u5907\u7F16\u53F7", "\u8BBE\u5907\u540D\u79F0", "\u73B0\u6709\u5E93\u5B58",
							"\u6700\u5927\u5E93\u5B58", "\u6700\u5C0F\u5E93\u5B58", "\u5E93\u5B58\u603B\u6570" }));
			scrollPane.setViewportView(table);
		}
		return device;
	}

	/**
	 * 根据名称查询
	 * 
	 * @param scrollPane
	 */
	private List<Device> fillTableByName(JScrollPane scrollPane, String code) {
		// 创建对象
		DeviceDao dd = new DeviceDao();
		// 调用查找所有的方法
		List<Device> list = dd.findDeviceByName(code);
		if (list != null) {
			// 获取list长度
			int row = list.size();
			// 创建二维数组
			Object[][] obj = new Object[row][6];
			// 迭代遍历
			for (int i = 0; i < obj.length; i++) {
				// 取值
				Device device = list.get(i);
				// 赋值
				obj[i][0] = device.getDevice_code();
				obj[i][1] = device.getDevice_name();
				obj[i][2] = device.getNow_num();
				obj[i][3] = device.getHigh_num();
				obj[i][4] = device.getLow_num();
				obj[i][5] = device.getTotal_num();
			}

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u8BBE\u5907\u7F16\u53F7", "\u8BBE\u5907\u540D\u79F0", "\u73B0\u6709\u5E93\u5B58",
							"\u6700\u5927\u5E93\u5B58", "\u6700\u5C0F\u5E93\u5B58", "\u5E93\u5B58\u603B\u6570" }));
			scrollPane.setViewportView(table);
		}
		return list;
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
